package shoppingmall.reception.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.reception.dto.PageBean;
import shoppingmall.reception.dto.ProductVO;
import shoppingmall.reception.mapper.CommercialSpecificationMapper;
import shoppingmall.reception.mapper.ProductClassificationMapper;
import shoppingmall.reception.mapper.ProductMapper;
import shoppingmall.reception.pojo.CommercialSpecification;
import shoppingmall.reception.pojo.Product;
import shoppingmall.reception.pojo.ProductClassification;
import shoppingmall.reception.service.ProductService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 商品部分相关的服务-服务层接口
 */
@SuppressWarnings({"all"})
@Service
public class ProductServiceImpl implements ProductService {
    //前台商品分类mapper层实例
    @Autowired
    private ProductClassificationMapper productClassificationMapper;
    //前台商品规格mapper层实例
    @Autowired
    private CommercialSpecificationMapper commercialSpecificationMapper;

    //前台商品mapper层实例
    @Autowired
    private ProductMapper productMapper;

    /**
     * 分页查询所有的商品数据(包含商品分类名称,商品规格集合)
     */
    @Override
    public PageBean getProductList(int page, int pageSize) {
        //对查询索引进行处理
        int startIndex = (page - 1) * pageSize;
        //查询商品数据总条数
        int productCount = productMapper.getProductCount();
        //分页查询商品数据
        List<Product> productList = productMapper.getProductList(startIndex, pageSize);
        //查询所有的商品分类数据
        List<ProductClassification> allProductClassification = productClassificationMapper.getAllProductClassification();
        //查询所有的商品规格数据
        List<CommercialSpecification> allCommercialSpecification = commercialSpecificationMapper.getAllCommercialSpecification();

        //准备返回的productVO[]对象
        List<ProductVO> productVOList = new ArrayList<>();
        //处理返回的productVO对象
        for (int i = 0; i < productList.size(); i++) {
            //准备返回的ProductVO对象
            ProductVO productVO = new ProductVO();
            productVO.setProductId(productList.get(i).getCommodityId());
            productVO.setProductName(productList.get(i).getCommodityName());
            productVO.setProductDescription(productList.get(i).getCommodityDescription());
            productVO.setProductDiscount(productList.get(i).getCommodityDiscount());
            productVO.setProductSales(productList.get(i).getCommoditySales());
            productVO.setProductSaleMoney(productList.get(i).getCommoditySaleMoney());
            productVO.setProductClassificationId(productList.get(i).getCommodityCategoryId());
            productVO.setProductImgs(productList.get(i).getCommodityImgs());
            productVO.setCreateTime(productList.get(i).getCreateTime());
            productVO.setUpdateTime(productList.get(i).getUpdateTime());
            //处理商品分类名称
            for (int j = 0; j < allProductClassification.size(); j++) {
                if (productList.get(i).getCommodityCategoryId() == allProductClassification.get(j).getCategoryId()) {
                    productVO.setProductClassificationName(allProductClassification.get(j).getCategoryName());
                }
            }
            //对商品规格进行处理---->添加上商品规格
            ArrayList<CommercialSpecification> commercialSpecifications = new ArrayList<>();
            Iterator iterator = allCommercialSpecification.iterator();
            while (iterator.hasNext()) {
                CommercialSpecification commercialSpecification = (CommercialSpecification) iterator.next();
                if (commercialSpecification.getCommodityId() == productList.get(i).getCommodityId()) {
                    commercialSpecifications.add(commercialSpecification);
                }
            }
            productVO.setCommercialSpecificationList(commercialSpecifications);
            //加入productVOList
            productVOList.add(productVO);
        }

        //准备返回的pageBean对象
        PageBean pageBean = new PageBean(productCount, productVOList);
        return pageBean;
    }

    /**
     * 查询所有的商品数据(包含商品分类名称,商品规格集合)
     */
    @Override
    public PageBean getAllProductList() {
        //查询商品数据总条数
        int productCount = productMapper.getProductCount();
        //分页查询商品数据
        List<Product> productList = productMapper.getAllProductList();
        //查询所有的商品分类数据
        List<ProductClassification> allProductClassification = productClassificationMapper.getAllProductClassification();
        //查询所有的商品规格数据
        List<CommercialSpecification> allCommercialSpecification = commercialSpecificationMapper.getAllCommercialSpecification();

        //准备返回的productVO[]对象
        List<ProductVO> productVOList = new ArrayList<>();
        //处理返回的productVO对象
        for (int i = 0; i < productList.size(); i++) {
            //准备返回的ProductVO对象
            ProductVO productVO = new ProductVO();
            productVO.setProductId(productList.get(i).getCommodityId());
            productVO.setProductName(productList.get(i).getCommodityName());
            productVO.setProductDescription(productList.get(i).getCommodityDescription());
            productVO.setProductDiscount(productList.get(i).getCommodityDiscount());
            productVO.setProductSales(productList.get(i).getCommoditySales());
            productVO.setProductSaleMoney(productList.get(i).getCommoditySaleMoney());
            productVO.setProductClassificationId(productList.get(i).getCommodityCategoryId());
            productVO.setProductImgs(productList.get(i).getCommodityImgs());
            productVO.setCreateTime(productList.get(i).getCreateTime());
            productVO.setUpdateTime(productList.get(i).getUpdateTime());
            //处理商品分类名称
            for (int j = 0; j < allProductClassification.size(); j++) {
                if (productList.get(i).getCommodityCategoryId() == allProductClassification.get(j).getCategoryId()) {
                    productVO.setProductClassificationName(allProductClassification.get(j).getCategoryName());
                }
            }
            //对商品规格进行处理---->添加上商品规格
            ArrayList<CommercialSpecification> commercialSpecifications = new ArrayList<>();
            Iterator iterator = allCommercialSpecification.iterator();
            while (iterator.hasNext()) {
                CommercialSpecification commercialSpecification = (CommercialSpecification) iterator.next();
                if (commercialSpecification.getCommodityId() == productList.get(i).getCommodityId()) {
                    commercialSpecifications.add(commercialSpecification);
                }
            }
            productVO.setCommercialSpecificationList(commercialSpecifications);
            //加入productVOList
            productVOList.add(productVO);
        }
        //返回数据
        return new PageBean(productCount, productVOList);
    }

    /**
     * 查询对应分类下的商品数据(包含商品分类名称,商品规格集合)
     */
    public PageBean getProductListByClassification (int classificationId) {
        //分页查询商品数据
        List<Product> productList = productMapper.getProductListByClassification(classificationId);
        //查询所有的商品分类数据
        List<ProductClassification> allProductClassification = productClassificationMapper.getAllProductClassification();
        //查询所有的商品规格数据
        List<CommercialSpecification> allCommercialSpecification = commercialSpecificationMapper.getAllCommercialSpecification();

        //准备返回的productVO[]对象
        List<ProductVO> productVOList = new ArrayList<>();
        //处理返回的productVO对象
        for (int i = 0; i < productList.size(); i++) {
            //准备返回的ProductVO对象
            ProductVO productVO = new ProductVO();
            productVO.setProductId(productList.get(i).getCommodityId());
            productVO.setProductName(productList.get(i).getCommodityName());
            productVO.setProductDescription(productList.get(i).getCommodityDescription());
            productVO.setProductDiscount(productList.get(i).getCommodityDiscount());
            productVO.setProductSales(productList.get(i).getCommoditySales());
            productVO.setProductSaleMoney(productList.get(i).getCommoditySaleMoney());
            productVO.setProductClassificationId(productList.get(i).getCommodityCategoryId());
            productVO.setProductImgs(productList.get(i).getCommodityImgs());
            productVO.setCreateTime(productList.get(i).getCreateTime());
            productVO.setUpdateTime(productList.get(i).getUpdateTime());
            //处理商品分类名称
            for (int j = 0; j < allProductClassification.size(); j++) {
                if (productList.get(i).getCommodityCategoryId() == allProductClassification.get(j).getCategoryId()) {
                    productVO.setProductClassificationName(allProductClassification.get(j).getCategoryName());
                }
            }
            //对商品规格进行处理---->添加上商品规格
            ArrayList<CommercialSpecification> commercialSpecifications = new ArrayList<>();
            Iterator iterator = allCommercialSpecification.iterator();
            while (iterator.hasNext()) {
                CommercialSpecification commercialSpecification = (CommercialSpecification) iterator.next();
                if (commercialSpecification.getCommodityId() == productList.get(i).getCommodityId()) {
                    commercialSpecifications.add(commercialSpecification);
                }
            }
            productVO.setCommercialSpecificationList(commercialSpecifications);
            //加入productVOList
            productVOList.add(productVO);
        }
        //返回数据
        return new PageBean(productList.size(), productVOList);
    }

    /**
     * 根据商品id查询某个商品数据(包含商品分类名称,商品规格集合)
     */
    public ProductVO getProductInfoByProductId (int productId) {
        //根据商品id查询商品信息
        Product productInfo = productMapper.getProductInfoByProductId(productId);
        if(productInfo == null) return null;
        //根据商品id查询商品分类表中对应的商品分类名称
        ProductClassification productClassification = productClassificationMapper.getProductClassificationNameByCategoryId(productInfo.getCommodityCategoryId());
        //根据商品id查询商品规格表中的商品规格集合
        List<CommercialSpecification> commercialSpecificationList = commercialSpecificationMapper.getCommercialSpecificationListByProductId(productId);
        //准备返回前端的对象ProductVO
        ProductVO productVO = new ProductVO();
        productVO.setProductId(productInfo.getCommodityId());
        productVO.setProductName(productInfo.getCommodityName());
        productVO.setProductDescription(productInfo.getCommodityDescription());
        productVO.setProductDiscount(productInfo.getCommodityDiscount());
        productVO.setProductSales(productInfo.getCommoditySales());
        productVO.setProductSaleMoney(productInfo.getCommoditySaleMoney());
        productVO.setProductClassificationId(productInfo.getCommodityCategoryId());
        productVO.setProductImgs(productInfo.getCommodityImgs());
        productVO.setCreateTime(productInfo.getCreateTime());
        productVO.setUpdateTime(productInfo.getUpdateTime());
        productVO.setProductClassificationName(productClassification.getCategoryName());
        productVO.setCommercialSpecificationList(commercialSpecificationList);
        return productVO;
    }

    /**
     * 根据商品名称对商品进行模糊匹配搜索 (productName = ?)
     */
    @Override
    public PageBean getProductListByName(String productName) {
        //调用mapper层方法
        List<Product> productList = productMapper.getProductListByName(productName);
        //返回数据
        return new PageBean(productList.size(),productList);
    }
}
